package cn.ithers.blog.dao;

import cn.ithers.blog.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 19:25
 * @description:
 */
public interface UserDao extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {
    /**
     * 根据用户名查找
     *
     * @param userName
     * @return
     */
    User findOneByUserName(String userName);

    /**
     * 根据邮箱
     *
     * @param email
     * @return
     */
    User findOneByEmail(String email);

    /**
     * 通过userId查找
     *
     * @param userId
     * @return
     */
    User findOneById(String userId);

    /**
     * 通过修改用户的状态来删除用户
     *
     * @param userId
     * @return
     */
    @Modifying
    @Query(nativeQuery = true, value = "UPDATE tb_user SET state = '0' WHERE id = ?")
    int deleteUserByUserId(String userId);



    /**
     * 根据邮箱修改密码
     *
     * @param encode
     * @param email
     * @return
     */
    @Modifying
    @Query(nativeQuery = true, value = "UPDATE tb_user SET password = ? WHERE email = ?")
    int updatePasswordByEmaill(String encode, String email);

    /**
     * 根据id修改邮箱
     *
     * @param email
     * @param id
     * @return
     */
    @Modifying
    @Query(nativeQuery = true, value = "UPDATE tb_user SET email = ? WHERE  id = ?")
    int updateEmialById(String email, String id);
}
